package com.example.fp.cgc.mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.example.fp.cgc.entity.Message
import com.example.fp.cgc.entity.MessageState
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

/**
 * 对消息的操作
 * @author 蔡
 * @since 2020-02-25
 */
interface MessageMapper : BaseMapper<Message> {

    /**
     * 获取未读消息
     */
    @Select("select * from message where isRead = false and toId = #{uid} and fromId = #{oid}")
    fun getNewMessage(uid: Int, oid: Int): MutableList<Message>

    /**
     * 获得历史消息
     */
    @Select("select * from message where (toId = #{uid} and fromId = #{oid}) or (toId = #{oid} and fromId = #{uid}) ORDER BY messageDate")
    fun getHistory(uid: Int, oid: Int): MutableList<Message>

    @Update("update message set isRead = true where toId = #{uid} and fromId = #{oid}")
    fun setRead(uid: Int, oid: Int)

    /**
     * 根据聊天内容搜索
     */
    @Select("""
            <script>
            SELECT * FROM MESSAGE
            where (toId = #{uid} and fromId = #{oid}) or (toId = #{oid} and fromId = #{uid}) ORDER BY messageDate
                        <if test="start!=null and size!=null">
                            limit #{start},#{size}
                        </if>
            </script>
    """)
    fun historyById(uid: Int, oid: Int, start: Int, size: Int): MutableList<Message>

    /**
     * 根据聊天内容搜索
     */
    @Select("""
        <script>
            SELECT * FROM MESSAGE
            <where>
            <if test="text != null and text != '' ">
               messageText LIKE CONCAT('%',#{text},'%')
            </if>
            </where>
            and ((toId = #{uid} and fromId = #{oid}) or (toId = #{oid} and fromId = #{uid})) ORDER BY messageDate
            <if test="start!=null and size!=null">
                limit #{start},#{size}
            </if>
        </script>
    """)
    fun searchText(uid: Int, oid: Int, text: String, start: Int, size: Int): MutableList<Message>

    /**
     * 模糊搜索消息
     */
    @Select("""
        <script>
            select count(*) from MESSAGE
                 <where>
                    <if test="text != null and text != ''">
                         messageText LIKE CONCAT('%',#{text},'%')
                    </if>
                 </where>
                 and ((toId = #{uid} and fromId = #{oid}) or (toId = #{oid} and fromId = #{uid}))
        </script>
    """)
    fun getMessageCountByText(uid: Int, oid: Int, text: String): Int

    /**
     * 获取消息数量
     */
    @Select("select count(*) from MESSAGE where ((toId = #{uid} and fromId = #{oid}) or (toId = #{oid} and fromId = #{uid}))")
    fun getMessageCount(uid: Int, oid: Int): Int

    /**
     * 获取消息状态
     */
    @Select("Select fromId, count(*) as count from message where toId = #{uid} and isRead = false group by fromId")
    fun getReadState(uid: Int): List<MessageState>

    /**
     * 获取与某用户的最后一条聊天信息
     */
    @Select("Select * from message" +
            " where" +
            " (toId = #{uid} and fromId =#{fromId})" +
            " or" +
            " (toId = #{fromId} and fromId =#{uid})" +
            " order by messageDate DESC limit 1")
    fun getLastMessage(uid: Int, fromId: Int): List<Message>

    /**
     * 获取聊天过的用户id
     */
    @Select("""
        select distinct fromId id from message where toId = #{uid}
         UNION
        select distinct toId id from message where fromId = #{uid}
    """)
    fun getHistoryUser(uid: Int): List<Int>

    /**
     * 获取有未读消息的用户id
     */
    @Select("select distinct fromId from message where toId = #{uid} and isRead = false order by messageDate DESC")
    fun getHistoryUserUnread(uid: Int): List<Int>
}